package com.miguo.utils;

import android.content.Context;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.miguo.app.AppConfig;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.utils.Toast;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;

public class FavorUtils {

    public static void setFavorStatus(Context mContext, GoodsBean goods,
                                      ImageView favorView, ProgressBar progress, ImageView like,
                                      ImageView unlikeLeft, ImageView unlikeRight) {
        if (goods != null) {
            if (!isFavorite(mContext, goods.getItem_id())) {
                addGoods(mContext, goods, favorView, progress, like);
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_24);
            } else {
                progress.setVisibility(View.VISIBLE);
                favorView.setVisibility(View.GONE);
                cancelFavoriteGoods(mContext,
                        String.valueOf(System.currentTimeMillis()), goods,
                        favorView, progress, unlikeLeft, unlikeRight);
                Statistics.onTrackCustomClick(mContext,
                        AppConfig.STATISTICS.EVENT_NAME_25);
            }
            return;
        }
        favorView.setVisibility(View.GONE);
        progress.setVisibility(View.GONE);
    }

    private static void addGoods(Context mContext, GoodsBean goods,
                                 ImageView favorView, ProgressBar progress, ImageView like) {
        progress.setVisibility(View.VISIBLE);
        favorView.setVisibility(View.GONE);
        addFavoriteGoods(mContext, String.valueOf(System.currentTimeMillis()),
                goods, favorView, progress, like);
    }

    public static void addFavoriteGoods(Context mContext, String datatime,
                                        GoodsBean goods, ImageView favorView, ProgressBar progress,
                                        final ImageView like) {

        if (goods == null) {
            Toast.show(mContext, "收藏失败，请稍后再试");
            return;
        }

        if (goods != null) {
            GoodsDB.getInstance(mContext).save(goods);

            // 调用失败
            // Toast
            // .show(mContext, "--网络异常，请稍后再试--" + goods.getTitle(), true);
            // favorView.setVisibility(View.VISIBLE);
            // progress.setVisibility(View.GONE);

            // 调用成功
            favorView.setVisibility(View.VISIBLE);
            progress.setVisibility(View.GONE);
            FavorUtils.showFavorToast(true, mContext, goods, favorView);
            like.setVisibility(View.VISIBLE);
            TranslateAnimation translateAnimation = new TranslateAnimation(
                    0.0F, 0.0F, 0.0F, -150.0F);
            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 0.0F);
            alphaAnimation.setAnimationListener(new AnimationListener() {
                public void onAnimationEnd(Animation animation) {
                    like.setVisibility(View.GONE);
                }

                public void onAnimationRepeat(Animation animation) {
                }

                public void onAnimationStart(Animation animation) {
                }
            });
            translateAnimation.setDuration(600L);
            alphaAnimation.setDuration(600L);
            RotateAnimation rotateAnimation1 = new RotateAnimation(0.0F,
                    -15.0F, 1, 0.5F, 1, 0.5F);
            rotateAnimation1.setDuration(300L);
            rotateAnimation1.setFillAfter(true);
            RotateAnimation rotateAnimation2 = new RotateAnimation(0.0F, 15.0F,
                    1, 0.5F, 1, 0.5F);
            rotateAnimation2.setFillAfter(true);
            rotateAnimation2.setDuration(300L);
            rotateAnimation2.setStartOffset(300L);
            AnimationSet animationSet = new AnimationSet(true);
            animationSet.addAnimation(translateAnimation);
            animationSet.addAnimation(alphaAnimation);
            // animationSet.addAnimation(rotateAnimation1);
            // animationSet.addAnimation(rotateAnimation2);
            animationSet.start();
            animationSet.startNow();
            like.startAnimation(animationSet);
            return;
        }
    }

    public static void cancelFavoriteGoods(Context mContext, String datatime,
                                           GoodsBean goods, ImageView favorView, ProgressBar progress,
                                           final ImageView unlikeLeft, final ImageView unlikeRight) {

        if (goods == null) {
            Toast.show(mContext, "取消收藏失败");
            return;
        }
        if (goods != null) {

            GoodsDB.getInstance(mContext).delete(goods.getItem_id(), 1);

            // 失败调用
            // favorView.setVisibility(View.VISIBLE);
            // progress.setVisibility(View.GONE);

            // 成功调用
            favorView.setVisibility(View.VISIBLE);
            progress.setVisibility(View.GONE);

            unlikeLeft.setVisibility(View.VISIBLE);
            unlikeRight.setVisibility(View.VISIBLE);
            FavorUtils.showFavorToast(false, mContext, goods, favorView);

            TranslateAnimation translateAnimation1 = new TranslateAnimation(
                    0.0F, -15.0F, 0.0F, 0.0F);
            translateAnimation1.setDuration(100L);
            translateAnimation1.setFillAfter(true);
            RotateAnimation rotateAnimation1 = new RotateAnimation(0.0F,
                    -15.0F, 1, 0.5F, 1, 0.5F);
            rotateAnimation1.setDuration(100L);
            rotateAnimation1.setFillAfter(true);
            TranslateAnimation translateAnimation2 = new TranslateAnimation(
                    0.0F, 15.0F, 0.0F, 0.0F);
            translateAnimation2.setDuration(100L);
            translateAnimation2.setFillAfter(true);
            RotateAnimation rotateAnimation2 = new RotateAnimation(0.0F, 15.0F,
                    1, 0.5F, 1, 0.5F);
            rotateAnimation2.setDuration(100L);
            rotateAnimation2.setFillAfter(true);
            TranslateAnimation translateAnimation3 = new TranslateAnimation(
                    0.0F, 0.0F, 0.0F, CommonUtils.dip2px(mContext, 60.0F));
            translateAnimation3.setDuration(300L);
            translateAnimation3.setStartOffset(100L);
            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0F, 0.0F);
            alphaAnimation.setDuration(300L);
            alphaAnimation.setStartOffset(100L);
            alphaAnimation
                    .setAnimationListener(new Animation.AnimationListener() {
                        public void onAnimationEnd(Animation animation) {
                            unlikeLeft.setVisibility(View.GONE);
                            unlikeRight.setVisibility(View.GONE);
                        }

                        public void onAnimationRepeat(Animation animation) {
                        }

                        public void onAnimationStart(Animation animation) {
                        }
                    });
            AnimationSet animationSet1 = new AnimationSet(true);
            animationSet1.addAnimation(translateAnimation1);
            // animationSet1.addAnimation(rotateAnimation1);
            animationSet1.addAnimation(alphaAnimation);
            animationSet1.addAnimation(translateAnimation3);
            animationSet1.start();
            unlikeLeft.startAnimation(animationSet1);
            AnimationSet animationSet2 = new AnimationSet(true);
            animationSet2.addAnimation(translateAnimation2);
            // animationSet2.addAnimation(rotateAnimation2);
            animationSet2.addAnimation(alphaAnimation);
            animationSet2.addAnimation(translateAnimation3);
            animationSet2.start();
            unlikeRight.startAnimation(animationSet2);
            return;
        }
    }

    private static void showFavorToast(boolean favor, Context mContext,
                                       GoodsBean goods, ImageView favorView) {
        if (favor) {
            Toast.show(mContext, "收藏成功");
            favorView.setBackgroundResource(R.drawable.ic_favor_sel);
            return;
        }
        Toast.show(mContext, "收藏已取消");
        favorView.setBackgroundResource(R.drawable.ic_favor_nor);
    }

    /* 是否已收藏 */
    public static boolean isFavorite(Context mContext, Long item_id) {
        boolean mboolean = false;
        if (item_id != null) {
            mboolean = GoodsDB.getInstance(mContext).isSave(item_id, 1);
        }
        return mboolean;
    }

}
